/*
 * 构建应用的根模块
 * 并做全局设置，简化后续开发的工作
 * 
 * @author yangw
 * @since 1.0.0
 */
var app = angular.module('sweet', [   
    'ngResource',		//注入由angular-resource.js提供的$resource来替代原生$http服务
    'restfull',         //针对$resource的Restfull封装（base包中restfull.js）
    'util',             //诸如dialog，tips等各种基础工具（base包中的util.js）
    'ngStorage',		//注入由ngStorage.js提供的$localStorage和$sessionStorage服务
    'ui.router',		//注入由angular-ui-router.js提供的$stateProvider和$urlRouterProvider服务    
    'ui.load',			//注入由ui-load.js(base包中)提供的服务   
    'oc.lazyLoad',		//注入由ocLazyLoad.js提供的$ocLazyLoadProvider服务        
    'ui.validate',		//注入由ui-validate.js(base包中，重写ui-util.js中的指令)提供的uiValidate指令     
    'ui.bootstrap',		//注入由ui-bootstrap-tpls.js提供的bootstrap样式或指令服务
    'ui.jq',			//注入由ui-jq.js(base包中，重写ui-util.js中的指令)提供的uiJq指令 
    'ngAnimate',		//注入由angular-animate.js提供的服务
    'toaster',			//注入消息提醒服务，由angualr-toaster.js提供
    'ngTouch',			//注入由angular-touch.js提供的$swipe服务或指令，注册和管理手机dom事件。
    'ngSanitize',		//注入由angular-sanitize.js提供的$sanitize服务或过滤器，解决ng-bind-html问题
    'ngCookies',  		//注入由angular-cookies.js提供的 $cookie和 $cookieStore服务
    
    'sweet.uikits', 'sweet.table', 'sweet.standard', 'sweet.backcomponent'   
])
.config(function ($controllerProvider,   $compileProvider,   $filterProvider,   $provide) {       
        app.controller = $controllerProvider.register; //直接使用app.controller()声明控制层
        app.directive  = $compileProvider.directive;
        app.filter     = $filterProvider.register;
        app.factory    = $provide.factory;
        app.service    = $provide.service;
        app.constant   = $provide.constant;
        app.value      = $provide.value;
    }
)
.run(function($log, $rootScope, $resource, util){	
	/*前端应用初始化用户登录信息*/
    $resource(encodeURL('/main/loginuserinfo')).get(function(resp) {
    	$rootScope.loginuser = resp.datas.loginuser;
    	$rootScope.loginuser.userOrgname = resp.datas.orgname;
    	$rootScope.loginuser.userTenantname = resp.datas.tenantname;
    });
    
    /*消息推送（长轮询）*/
	var failConn = 0;
    var pollMsg = function(){
    	$.ajax({
			url : encodeURL('/push/msgpoll?t=' + (new Date()).valueOf()), 
	        success : function(resp) {	        	
	        	if(resp.statusCode != '503'){ //非超时情况
	        		if(resp.datas.msgflag == 'log'){
	        			if($rootScope.pushmsg_log != undefined){
	        				$rootScope.pushmsg_log(resp);
	        			}
	        		}else if(resp.datas.msglevel != null && resp.datas.msglevel != undefined){
	        			util.message(resp.datas.msglevel, '您有新的消息', resp.i18nMessage);
	        		}
	        	}
	        },
	        error: function (XMLHttpRequest, textStatus, errorThrown){
	        	failConn++;
	        	if(errorThrown == ""){//防止刷新引起的二次请求
	        		failConn = 10;
	        	};
	        },
	        complete: function(XMLHttpRequest, textStatus) {
	        	XMLHttpRequest = null; //回收对象				
				if (failConn >= 10) {  //暂停
					failConn = 0;
					setTimeout(function () {
						pollMsg();
			        }, 60000);
				}else {
					pollMsg();
				}
	        }
	    });
    };
    pollMsg();  
});

var backcomponent = angular.module('sweet.backcomponent', [])
.config(function ($controllerProvider, $provide) {       
		backcomponent.controller = $controllerProvider.register; 
		backcomponent.factory    = $provide.factory;
   }
);

var standard = angular.module('sweet.standard', [])
.config(function ($controllerProvider, $provide) {       
		standard.controller = $controllerProvider.register; 
		standard.factory    = $provide.factory;
   }
);

var uikits = angular.module('sweet.uikits', [])
.config(function ($controllerProvider) {       
		uikits.controller = $controllerProvider.register; 
    }
);

var table = angular.module('sweet.table', [])
 .config(function ($controllerProvider) {       
	 	table.controller = $controllerProvider.register; 
    }
);
  